package com.one.rope.mvp.web.business.service.impl;

import com.one.rope.mvp.basecore.util.JsonUtils;
import com.one.rope.mvp.basecore.util.MyBeanUtils;
import com.one.rope.mvp.basecore.util.StringUtils;
import com.one.rope.mvp.web.business.entity.CompanyMember;
import com.one.rope.mvp.web.business.dao.CompanyMemberMapper;
import com.one.rope.mvp.web.business.service.ICompanyMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.one.rope.mvp.web.common.dto.admin.team.SaveTeamMemberDTO;
import com.one.rope.mvp.web.common.dto.admin.team.TeamMemberDetailVO;
import com.one.rope.mvp.web.common.dto.admin.team.MemberStudyExperienceItemVO;
import com.one.rope.mvp.web.common.dto.admin.team.MemberWorkExperienceItemVO;
import com.one.rope.mvp.basecore.bean.AdminBusinessMessage;
import com.one.rope.mvp.basecore.bean.ResponseObject;
import com.one.rope.mvp.basecore.exception.BusinessDataException;
import java.util.ArrayList;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 企业团队成员信息表 服务实现类
 * </p>
 *
 * @author wesley
 * @since 2020-02-20
 */
@Service
public class CompanyMemberServiceImpl extends ServiceImpl<CompanyMemberMapper, CompanyMember> implements ICompanyMemberService {

  @Override
  public ResponseObject saveTeamMember(SaveTeamMemberDTO requestDTO) {
    CompanyMember companyMember = new CompanyMember();
    MyBeanUtils.copyPropertiesIgnoreNull(requestDTO, companyMember);
    List<MemberWorkExperienceItemVO> workExperienceItemVOList = requestDTO.getWorkExperienceList();
    if (null != workExperienceItemVOList && workExperienceItemVOList.size() > 0) {
      companyMember.setWorkExperience(JsonUtils.objectToFasterxmlJsonString(requestDTO.getWorkExperienceList()));
    }

    List<MemberStudyExperienceItemVO> studyExperienceItemVOList = requestDTO.getStudyExperienceList();
    if (null != studyExperienceItemVOList && studyExperienceItemVOList.size() > 0) {
      companyMember.setStudyExperience(JsonUtils.objectToFasterxmlJsonString(requestDTO.getStudyExperienceList()));
    }
    if(companyMember.getId() == 0){
      companyMember.setId(null);
    }
    this.saveOrUpdate(companyMember);
    // 设置资料完善度
    setPerfectDegree(companyMember.getId());
    return ResponseObject.successObject();
  }

  @Async
  public void setPerfectDegree(Long id) {
    CompanyMember companyMember = this.getById(id);
    // 基础值 (姓名、称谓、生日、任职岗位必填 占比30%)
    int initDegree = 30;
    if (null != companyMember) {
      /**
       * 工作经历、教育经历 各20%，有一条数据就算
       * 擅长技能和资源，10%
       * 创业经验，10%
       * 一句话介绍，10%
       */
      if (!StringUtils.isNullOrEmpty(companyMember.getWorkExperience())) {
        initDegree = initDegree + 20;
      }
      if (!StringUtils.isNullOrEmpty(companyMember.getStudyExperience())) {
        initDegree = initDegree + 20;
      }
      if (!StringUtils.isNullOrEmpty(companyMember.getSkilled())) {
        initDegree = initDegree + 10;
      }
      if (null != companyMember.getBusinessExperience()
          && companyMember.getBusinessExperience() != 0) {
        initDegree = initDegree + 10;
      }
      if (!StringUtils.isNullOrEmpty(companyMember.getIntroduction())) {
        initDegree = initDegree + 10;
      }
      companyMember.setPerfectDegree(initDegree + "%");
      this.updateById(companyMember);
    }
  }

  @Override
  public ResponseObject deleteTeamMember(Long id) {
    CompanyMember companyMember = this.getById(id);
    if (null == companyMember) {
      throw new BusinessDataException(AdminBusinessMessage.TEAM_MEMBER_NOT_EXIST);
    }
    this.removeById(id);
    return ResponseObject.successObject();
  }

  @Override
  public ResponseObject teamMemberDetail(Long id) {
    CompanyMember companyMember = this.getById(id);
    if (null == companyMember) {
      throw new BusinessDataException(AdminBusinessMessage.TEAM_MEMBER_NOT_EXIST);
    }
    TeamMemberDetailVO resultVO = new TeamMemberDetailVO();
    MyBeanUtils.copyPropertiesIgnoreNull(companyMember, resultVO);

    List<String> skilledList = new ArrayList<>();
    List<MemberWorkExperienceItemVO> workExperienceList = new ArrayList<>();
    List<MemberStudyExperienceItemVO> studyExperienceList = new ArrayList<>();
    // 设置擅长技能
    String skilled = companyMember.getSkilled();
    if (!StringUtils.isNullOrEmpty(skilled)) {
      skilledList = CollectionUtils.asList(skilled.split(","));
    }

    // 设置工作经验
    String workExperience = companyMember.getWorkExperience();
    if (!StringUtils.isNullOrEmpty(workExperience)) {
      workExperienceList = JsonUtils.stringToList(workExperience, MemberWorkExperienceItemVO.class);
    }

    // 设置教育经验
    String studyExperience = companyMember.getStudyExperience();
    if (!StringUtils.isNullOrEmpty(studyExperience)) {
      studyExperienceList = JsonUtils.stringToList(studyExperience, MemberStudyExperienceItemVO.class);
    }

    resultVO.setSkilledList(skilledList);
    resultVO.setWorkExperienceList(workExperienceList);
    resultVO.setStudyExperienceList(studyExperienceList);
    return new ResponseObject(resultVO);
  }
}
